function login() {
	var url = "/login";
	var errorMessage = 'field cannot left blank.';
	var emailIdField = document.getElementById('emailid');
	var emailIdValid = isValid(emailIdField, document.getElementById('emailiderror'), errorMessage) && verifyEmailId(emailIdField);
	var passwordValid = isValid(document.getElementById('password'), document.getElementById('passworderror'), errorMessage);
	if (emailIdValid && passwordValid) {
		var args;
		args = "parameter={'emailid':'" + document.getElementById('emailid').value + "',";// emailBox
		args += "'password':" + document.getElementById('password').value + "'}"; // orgname
		callServlet(url, args);
		alert('servlet called');
	}

}

function onFocusGain(field) {
	var errorFieldId = field.id + 'error';
	clearErrorMessage(document.getElementById(errorFieldId));
	field.style.border = "2px solid blue";
}
function onFocusLost(field) {
	clearErrorBorder(field);
}
function isValid(field, errorMessageField, errorMessage) {
	if (field.value != null && field.value.length > 0) {
		clearErrorMessage(errorMessageField);
		clearErrorBorder(field);
		return true;
	} else {
		showErrorBorder(field);
		showErrorMessage(errorMessageField, errorMessage);
		return false;
	}
}

function showErrorBorder(field) {
	field.style.border = '1px solid red';
}
function showErrorMessage(field, message) {
	field.innerHTML = message;
	field.style.display = 'block';
}
function clearErrorMessage(field) {
	field.innerHTML = '';
	field.style.display = 'none';
}
function clearErrorBorder(field) {
	field.style.border = '1px solid gray';
}
function callServlet(url, args) {
	alert('url : ' + url + ',args : ' + args);
	var xmlHttp;
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		alert("Your browser does not support XMLHTTP!");
		return;
	}
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", args.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			var statusText = xmlHttp.statusText;
			var str = xmlHttp.responseText;
			if (xmlHttp.statusText == 'Internal Server Error') {
				var startIndex = str.indexOf("@exst@");
				var endIndex = str.indexOf("@exen@");
				alert(startIndex + "," + endIndex);
				var length = endIndex - startIndex - 6;
				var msg = str.substr(startIndex + 6, length);
				alert('Exception from server[' + msg + ']');
			}
			responseReceived(str);
		}
	};
	xmlHttp.send(args);
}

function responseReceived(responseText) {
	alert("resp : " + responseText);
}

function GetXmlHttpObject() {
	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject) {
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function verifyEmailId(field) {
	var pattern = /[\w]+((.|-)[\w]+)*@[\w-]+(.[\w-]+)+/;
	var valid = (pattern.test(field.value));
	if (!valid) {
		showErrorBorder(field);
		showErrorMessage(document.getElementById('emailiderror'), 'invalid emailid');
		return false;
	}
	return true;
}
